﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using ATEC.ATDataCoreNET;
using ATEC.ATDataCoreNET.ATCCL;
using ATEC.GeometryNET;
using System.Threading.Tasks;
using System.Windows.Forms;
using DefinePropsInput;
using ATEC.CCLObj.Entity;

namespace PropsItemExample
{
    public partial class PropsItemTestForm : Form
    {
        public PropsItemTestForm()
        {
            InitializeComponent();
        }

        private string libraryName = "PropsItemLibarayTest";
        private string itemTypeName = "PropsItemExmaple";
        private ATEC.ATDataCoreNET.ATObjects.ATObject hostobj = null;


        private string currProp;
        /// <summary>
        /// 创建itemtype
        /// </summary>
        private void button_Create_Click(object sender, EventArgs e)
        {
            ATSceneRef scene = SSDPNET.ScenesFunctions.SceneRefGetActive();
            ATProject file = scene.GetATProject();

            DefineProps view = new DefineProps();
            Nullable<bool> ret = view.ShowDialog();
            if (ret == true)
            {

                // 查找 Library
                PropsItemLibrary itemTypeLibrary = PropsItemLibrary.FindByName(libraryName, file);
                if (itemTypeLibrary == null)
                {
                    // 没有则创建Library
                    itemTypeLibrary = PropsItemLibrary.Create(libraryName, file);
                }

                // 获取输入的属性类型名称
                itemTypeName = view.Input_PropClassT;

                // 获取属性类型
                PropsItem itemType = itemTypeLibrary.GetPropsItemByName(itemTypeName);
                if (itemType == null)
                {
                    // 没有则创建
                    itemType = itemTypeLibrary.AddPropsItem(itemTypeName);
                }

                // 获取输入的属性项名称
                string propName = String.IsNullOrEmpty(view.Input_PropName) ? "默认名称" : view.Input_PropName;
                currProp = propName;

                // 查找属性
                CustomProperty testProperty = itemType.GetPropertyByName(propName);
                if(testProperty != null)
                {
                    itemType.RemoveProperty(testProperty);
                    itemTypeLibrary.Write();
                }
                // 增加属性
                testProperty = itemType.AddProperty(propName, true);
                testProperty.Type = CustomProperty.TypeKind.String;
                testProperty.DefaultValue = String.IsNullOrEmpty(view.Input_PropValue) ? "默认值": view.Input_PropValue;

                // 写入属性到Library
                itemTypeLibrary.Write();
            }
        }

        /// <summary>
        /// 创建一个线串，并挂载itemtype的属性
        /// </summary>
        private void button_CreateLine_Click(object sender, EventArgs e)
        {
            if (hostobj == null || hostobj.IsDeleted || !hostobj.IsValid)
            {
                //创建线串
                XPoint3d p0 = XPoint3d.Zero;
                XPoint3d p1 = new XPoint3d(150, 0, 0);
                p0.X = -150;
                p0.Y = 20;
                p1.Y = 20;
                XSegment3d line = new XSegment3d(p0, p1);
                CurveSet.BoundaryType BndryType = CurveSet.BoundaryType.None;
                CurveSet curveSetLine = CurveSet.Create(BndryType);
                curveSetLine.Add(BasicCurve.CreateLine(line));

                if (curveSetLine == null)
                {
                    return;
                }
                //获取当前激活Scene
                ATSceneRef scene = SSDPNET.ScenesFunctions.SceneRefGetActive();

                //将直线显示在视口
                hostobj = ATEC.ATDataCoreNET.ATObjects.GeometryToATObject.ToObject(scene.GetATScene(), curveSetLine, null);
                ObjectPropertiesSetter setter = new ObjectPropertiesSetter();
                setter.SetColor((uint)3);
                setter.Apply(hostobj);
                StatusInt n = hostobj.AddToScene();
            }

            if (hostobj != null)
            {
                //获取当前文件
                ATProject file = ATEC.ATCore.Session.Entity.GetActiveATProject();

                // 查找属性库
                PropsItemLibrary itemTypeLibrary = PropsItemLibrary.FindByName(libraryName, file);
                if (itemTypeLibrary == null)
                    return;

                // 查找指定名称的属性类型
                PropsItem itemType = itemTypeLibrary.GetPropsItemByName(itemTypeName);
                if (itemType == null)
                    return;

                // ATObject -> 属性Host.
                CustomPropsItemHost host = new CustomPropsItemHost(hostobj, true);
                // 应用属性
                IATCCLEntity entity= host.ApplyCustomPropsItem(itemType, true);
                if (entity == null)
                    return;
                entity.ScheduleChanges(hostobj); // 标记变化, 在 hostObj 写入时写入变化.

                // 更新对象
                hostobj.ReplaceInScene(hostobj);
            }
            MessageBox.Show("创建成功");
        }

        /// <summary>
        /// 查询相关属性
        /// </summary>
        private void button_Query_Click(object sender, EventArgs e)
        {
            // ATObject -> Host
            CustomPropsItemHost host = new CustomPropsItemHost(hostobj, true);
            // 根据缓存的库名、类型名、属性名，取属性值
            IATCCLEntity entity = host.GetCustomPropsItem(libraryName, itemTypeName);
            string mess = string.Format("最近一次设置：{0}={1}", currProp, entity[currProp].NativeValue);
            MessageBox.Show(mess);

            // 遍历属性
            List<string> lstStr = new List<string>();
            foreach(IATCCLEntity ccl in host.CustomItems) // 每一个属性类型
            {
                lstStr.Add(String.Format("{0}:", ccl.ClassDefinition.Name)); 
                foreach(ICCLPropsValue propV in ccl) // 每一个属性项
                {
                    lstStr.Add(String.Format("\t{0}={1}", propV.AccessString, propV.NativeValue));
                }
            }
            string full = String.Join("\n", lstStr.ToArray());
            MessageBox.Show(full);
        }
        //参照场景
        private void btn_AttachScne_Click(object sender, EventArgs e)
        {
            AddPropToObjInAttachExample.CreateSceneAndAttachToScene3D();
        }

        private void btn_AddPropToObjInAttach_Click(object sender, EventArgs e)
        {
            AddPropToObjInAttachExample.AddPropToAttachScene();
        }
    }
}
